package com.AI.SDK.Client;

import com.aliyun.broadscope.bailian.sdk.AccessTokenClient;
import com.aliyun.broadscope.bailian.sdk.ApplicationClient;
import com.aliyun.broadscope.bailian.sdk.models.BaiLianConfig;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsRequest;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
@Data
@Slf4j
public class BaiLianSDK {
    private String accessKey;
    private String secretKey;

    public BaiLianSDK(String accessKey,String secretKey){
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public static void main(String[] args) {
        String accessKeyId = "LTAI5tEqhMw9VP83Rm9xb54d";
        String accessKeySecret = "vbgDlknODJSy4wggHvkTEGDzKo33TG";
        String agentKey = "30161e06a9544fe9b22b3261b86a3e60_p_efm";

        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();

        String appId = "4d7710dd5b894a05b7ec79e81b4003d9";
        BaiLianConfig config = new BaiLianConfig()
                .setApiKey(token);

        String prompt = "给我解释一下红黑树";
        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setPrompt(prompt)
                .setHasThoughts(true)
                .setSessionId("******"); //多轮对话, 多次请求如果session id相同，则后续对话自动保存之前的对话上下文, sessionId尽量采用uuid, 如String sessionId = UUIDGenerator.generate();

        ApplicationClient client = new ApplicationClient(config);
        client.streamCompletions(request, new ApplicationClient.StreamEventListener() {
            @Override
            public void onOpen() {
                System.out.println("打开连接");
            }

            @Override
            public void onClosed() {
                System.out.println("关闭连接");
            }

            @Override
            public void onEvent(CompletionsResponse response) {
                System.out.println("流式数据, data=" + response.getData().getText());
            }

            @Override
            public void onFailure(@Nullable Throwable t, int code, String body) {
                String errMsg = t == null ? "" : t.getMessage();
                System.out.println("onFailure, code=" + code + ", body=" + body + ", err: " + errMsg);
            }
        });
    }
}
